<?php

class Api_UserController extends Zend_Rest_Controller
{

    public function init()
    {
        require_once 'Zend/Db.php';
        $this->db = Zend_Db::factory('Mysqli',
        array(
        'host' => 'localhost',
        'dbname' => 'zooxanth_mystuff',
        'username'=> 'zooxanth_mystuff',
        'password'=> 'wecandoit25')
        );
        //$this->db = Zend_Db::factory($config->resources->db);
        $this->no_results = array('status' => 'NO_RESULTS');
    }
    
    /**
     * List
     *
     * The index action handles index/list requests; it responds with a
     * list of the requested resources.
     * 
     * @return json
     */
    public function indexAction()
    {
        $data = $this->db->fetchAll('SELECT id,realname FROM user');
        /*
        foreach ($data as $row) {
            echo $row['realname'];
        }
         *
         */
        // do some processing...
        // Send the JSON response:
        $this->_helper->json($data);
    }
    // 1.9.2 fix
    public function listAction() { return $this->_forward('index'); }

    /**
     * View
     *
     * The get action handles GET requests and receives an 'id' parameter; it 
     * responds with the server resource state of the resource identified
     * by the 'id' value.
     * 
     * @param integer $id
     * @return json
     */
    public function getAction()
    {
        $id = $this->_getParam('id');
        $data = $this->db->fetchRow('SELECT * FROM user WHERE id='.$id);

        // do some processing...
        // Send the JSON response:
        $this->_helper->json($data);
    }

    /**
     * Create
     *
     * The post action handles POST requests; it accepts and digests a
     * POSTed resource representation and persists the resource state.
     * 
     * @param integer $id
     * @return json
     */
    public function postAction()
    {
         echo "POST";
        //Check if item and userId are being sent
        if($this->_hasParam('item') && $this->_hasParam('uid')){
            $itemId = $this->_getParam('item');
            $userId = $this->_getParam('uid');
            
            //Dont show anything
            $this->_helper->viewRenderer->setNoRender(true);
            // do some processing...
            // Send the JSON response:
        }else{
            $this->_helper->json($this->no_results);
        }

        // do some processing...
        // Send the JSON response:
    }

    /**
     * Update
     *
     * The put action handles PUT requests and receives an 'id' parameter; it 
     * updates the server resource state of the resource identified by 
     * the 'id' value.
     * 
     * @param integer $id
     * @return json
     */
    public function putAction()
    {
        //Check if item and userId are being sent
        $itemId = $this->getRequest()->getParam('item');
        $userId = $this->getRequest()->getParam('uid');
        
        if (!empty($itemId) && !empty($userId) ){
            $itemValues = $this->db->fetchRow('SELECT name,company_id FROM item WHERE id='.$itemId);
            $itemName = $itemValues['name'];
            $itemCompanyId = $itemValues['company_id'];
            
            $userRealName = $this->db->fetchOne('SELECT realname FROM user WHERE id='.$userId);
            $companyName = $this->db->fetchOne('SELECT name FROM company WHERE id='.$itemCompanyId);
            $msg= $userRealName." just bought a ".$companyName." :: ".$itemName;
            
            $data = array(
                'user_id' => $userId,
                'item_id' => $itemId
            );

            $this->db->insert('user_item', $data);
            
            $this->_helper->viewRenderer->setNoRender(true);
            $this->getResponse()
            ->setBody($msg);
        }else{
            // do some processing...
            $this->_helper->json($this->no_results);
        }
    }

    /**
     * Delete
     *
     * The delete action handles DELETE requests and receives an 'id' 
     * parameter; it updates the server resource state of the resource
     * identified by the 'id' value.
     * 
     * @param integer $id
     * @return json
     */
    public function deleteAction()
    {
        $id = $this->_getParam('id', 0);

        // do some processing...
        // Send the JSON response:
        $this->_helper->json($this->no_results);
    }
}

